// components/maudio/maudio.js
let player
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    src:{
      type:String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgsrc: '../../images/start.png',
    isStop: false,
    playProgress: 0,
    position:0,
    time: '',
    current: '',
    isFirst:true,
    player: {},
    maxCount: 50, // 绘制一个圆环所需的步骤
  },
  /**
   * 组件的方法列表
   */
  methods: {
    init() {

      this.circle = this.selectComponent("#circle");
      // 绘制背景圆环
      this.circle.drawCircleBg('circle_bg', 25, 2);
      player = wx.createInnerAudioContext()
      player.src = this.properties.src
      player.onPlay(res => {
        // if (this.data.isFirst) {
        //   player.pause();
        //   this.setData({
        //     isFirst: false,
        //   })
        // }
        console.log("ok")
      })
      player.onTimeUpdate(() => {
        this.audioTimeUpdate()
      })
      player.onEnded(() => {
        this.setData({
          imgsrc: '../../images/start.png',
          isStop: false
        })
      })
    },
    /**
    * 控制图标显示
    */
    toggle() {
      if (this.data.isStop) {
        this.setData({
          imgsrc: '../../images/start.png',
          isStop: false
        })
        player.pause()
      } else {
        this.setData({
          imgsrc: '../../images/stop.png',
          isStop: true
        })
        player.play()
      }
    },
    /**
     * 格式化时间
     */
    formatSeconds: function (value) {
      var secondTime = parseInt(value);// 秒
      var minuteTime = 0;// 分
      var hourTime = 0;// 小时
      if (secondTime > 60) {//如果秒数大于60，将秒数转换成整数
        //获取分钟，除以60取整数，得到整数分钟
        minuteTime = parseInt(secondTime / 60);
        //获取秒数，秒数取佘，得到整数秒数
        secondTime = parseInt(secondTime % 60);
        //如果分钟大于60，将分钟转换成小时
        if (minuteTime > 60) {
          //获取小时，获取分钟除以60，得到整数小时
          hourTime = parseInt(minuteTime / 60);
          //获取小时后取佘的分，获取分钟除以60取佘的分
          minuteTime = parseInt(minuteTime % 60);
        }
      }

      var result = parseInt(secondTime);
      var secondstr = result > 9 ? result : '0' + result;
      var minutestr
      var hourstr
      if (minuteTime > 0) {
        result = parseInt(minuteTime);
        minutestr = result > 9 ? result : '0' + result;
      } else {
        minutestr = ''
      }
      if (hourTime > 0) {
        result = parseInt(hourTime);
        hourstr = result > 9 ? result : '0' + result;
      } else {
        hourstr = ''
      }
      if (minutestr == '') {
        return '00:' + secondstr
      }
      if (hourstr == '') {
        return minutestr + ':' + secondstr
      }
      return hourstr + ':' + minutestr + ':' + secondstr
    }, 
    
    /**
     * sliderChange监听
     */
    sliderChange(e) {
      let playProgress = e.detail.value;
      let position = (playProgress / 100) * player.duration;
      this.setData({
        position
      })
      player.seek(position)
      this.circle.drawCircle('circle_draw', 25, 2, playProgress / this.data.maxCount)

    },
    /**
    * 播放更新处理函数
    */
    audioTimeUpdate:function () {
      let playProgress = player.currentTime / player.duration * 100;
      this.circle.drawCircle('circle_draw', 25, 2, playProgress / this.data.maxCount)
      this.setData({
        playProgress: playProgress,
        time: this.formatSeconds(player.duration),
        current: this.formatSeconds(player.currentTime)
      })
    },
    destroyAudio:function(){
      player.destroy()
    },
    pauseAudio:function(){
      player.pause()
    },
    pauseAudio:function(){
      this.setData({
        imgsrc: '../../images/start.png',
        isStop: false
      })
      player.pause()
    }
  },
})
